package com.linux.huhx.codetools.generate;

import com.linux.huhx.codetools.transutil.CodeToolUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.MessageFormat;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by huhx on 2017-06-26.
 */
public class GenerateMysql {
    private String FILE_NAME_PATH = "C:/Users/76801/Desktop/huhx2.txt";

    @Before
    public void createConn() {

    }

    @After
    public void cloaseConn() {

    }

    // resultMap的数据映射
    @Test
    public void generate_resultMap() {
        String requestDatagram = "<result property=\"{0}\" column=\"{1}\" />";
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            for (String string : listStrings) {
                string = string.split(" ")[0];
                String line = MessageFormat.format(requestDatagram, CodeToolUtils.transforFirstLetterUpper(string), string.toUpperCase()); // 这里面:注释、名字
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 插入数据库字段的语句
    @Test
    public void generate_insertCode() {
        String requestDatagram = "INSERT INTO\n\t{0}({1})\nVALUES\n\t({2})";
        File file = new File(FILE_NAME_PATH);
        try {
            List<String> listStrings = FileUtils.readLines(file);
            String tablesName = listStrings.get(0);
            String baseSql = "";
            String mapSql = "";
            String string = "";
            for (int i = 1; i < listStrings.size(); i++) {
                string = listStrings.get(i).split(" ")[0];
                baseSql += string + ", ";
                mapSql += "#{" + string + "}, ";
            }
            baseSql = baseSql.substring(0, baseSql.length() - 2);
            mapSql = mapSql.substring(0, mapSql.length() - 2);
            String line = MessageFormat.format(requestDatagram, tablesName.toUpperCase(), baseSql, mapSql);
            System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取mysql中对应schema的所有表
    @Test
    public void generate_alltables() {
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            while (true) {
                Socket socket = serverSocket.accept();
                TimeUnit.SECONDS.sleep(1);
                DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
                outToClient.writeBytes("hello" + new Random().nextInt(100));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 读取表中的字段数据
    @Test
    public void generate_columns() {
        try {
            Socket socket = new Socket("127.0.0.1", 9999);
            while (true) {
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println(inFromServer.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
